package com.qu.tabhost.tabs;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qu.tabhost.R;
import com.qu.tabhost.fragment.ChatFragment;
import com.qu.tabhost.fragment.NewsFragment;
import com.qu.tabhost.fragment.PictureFragment;
import com.qu.tabhost.fragment.VideoFragment;
import com.qu.tabhost.tabs.widget.NavigationButton;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//自定义tab
public class NavFragment extends Fragment implements View.OnClickListener{
    @BindView(R.id.nav_my)
    NavigationButton mNavChat;
    @BindView(R.id.nav_news)
    NavigationButton mNavNews;
    @BindView(R.id.nav_discover)
    NavigationButton mNavPicture;
    @BindView(R.id.nav_video)
    NavigationButton mNavVideo;

    private NavigationButton mCurrentNavButton;
    private FragmentManager fragmentManager;
    private int containerId;
    private Context mContext;
    private OnNavigationReselectListener onReselectListener;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_nav, container, false);
        ButterKnife.bind(this,view);
        initWidget(view);
        return view;
    }
    //初始化视图
    private void initWidget(View Root){
        mNavChat.init(R.drawable.tab_icon_chat,
                R.string.nav_my, ChatFragment.class);
        mNavNews.init(R.drawable.tab_icon_me,
                R.string.nav_news, NewsFragment.class);
        mNavPicture.init(R.drawable.tab_icon_picture,
                R.string.nav_discover, PictureFragment.class);
        mNavVideo.init(R.drawable.tab_icon_video,
                R.string.nav_video, VideoFragment.class);
    }
    public void setup(FragmentManager mFragmentManager,int containerId,Context context,
                      OnNavigationReselectListener mOnNavigationReselectListener){
        this.fragmentManager = mFragmentManager;
        this.containerId = containerId;
        this.mContext = context;
        this.onReselectListener = mOnNavigationReselectListener;

        //初始化
        clearOldFragment();
        doSelect(mNavChat);
        mNavChat.setTVdot(99);
    }
    //每次启动APP，我们都要清除FragmentManager中保存的Fragment的实例
    private void clearOldFragment(){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        List<Fragment> fragments = fragmentManager.getFragments();
        boolean isDoCommit = false;
        if(transaction == null || fragments == null || fragments.size() ==0){
            return;
        }
        for(Fragment fragment : fragments){
            if(fragment != this){
                transaction.remove(fragment);
                isDoCommit = true;
            }
        }
        if(isDoCommit){
            transaction.commitNow();
        }
    }

    //对每个被点击的NavButton做出对应的选择
    public void doSelect(NavigationButton newNavButton){
        NavigationButton oldNavButton = null;
        if(mCurrentNavButton != null){
            oldNavButton = mCurrentNavButton;
            if(newNavButton == oldNavButton){
                //如果点击相同的NavButton，要做重复的选择
                onReselect(newNavButton);
                return;
            }
            oldNavButton.setSelected(false);
        }
        newNavButton.setSelected(true);
        doChange(oldNavButton,newNavButton);
        mCurrentNavButton = newNavButton;
    }

    //对做出选择的改变做出对应的事件
    private void doChange(NavigationButton oldNavButton,NavigationButton newNavButton){
        FragmentTransaction ft = fragmentManager.beginTransaction();
        //移除oldNavButton与Activity的关联
        if(oldNavButton != null){
            if(oldNavButton.getFragment() != null){
                ft.detach(oldNavButton.getFragment());
            }
        }
        //对新的newNavButton与Activity做出关联，如果存在直接关联，不存在则创建。
        if(newNavButton != null){
            if(newNavButton.getFragment() == null){
                Fragment fragment = Fragment.instantiate(
                        mContext,newNavButton.getClx().getName(),null);
                ft.add(containerId,fragment,newNavButton.getTag());
                newNavButton.setFragment(fragment);
            }else{
                ft.attach(newNavButton.getFragment());
            }
        }
        ft.commit();
    }
    //对重复选择NavButton做出对应的事件
    private void onReselect(NavigationButton navButton){
       if(onReselectListener != null){
           onReselectListener.onReselect(navButton);
        }
    }
    @OnClick({R.id.nav_my,R.id.nav_news,R.id.nav_discover,R.id.nav_video})
    @Override
    public void onClick(View view) {
        if(view instanceof NavigationButton){
            NavigationButton navigationButton = (NavigationButton) view;
            doSelect(navigationButton);
        }
    }

    public interface OnNavigationReselectListener{
        void onReselect(NavigationButton navButton);
    }
}
